package service

import (
	"fmt"
	"net/http"
	"server-admin-api/dto"
	"server-admin-api/message"
	"server-admin-api/utils"
	"strconv"
)

type WebsocketService struct {
	service *dto.CustomizeDto
}

func NewWebsocketService() *WebsocketService {
	return &WebsocketService{}
}

func (w *WebsocketService) Message(id string, resp http.ResponseWriter, req *http.Request) error {

	w.service = dto.NewCustomizeDto()

	pk, err := strconv.Atoi(id)
	if err != nil {
		return message.NewSysError("获取数据失败")
	}

	data, err := w.service.FindOne(pk)
	if err != nil {
		return message.NewSysError("获取数据失败")
	}

	conn, err := utils.InitWsConn(resp, req, fmt.Sprintf("tail -f %s", data.LogPath))
	if err != nil {
		return message.NewSysError("构建websocket失败")
	}

	conn.Push()

	return nil
}
